﻿/*  
 * uncomment this to connect to the service running on http://test.jakubmaly.cz/travelagentservice.asmx
 * leave commented to connect to localhost   
 */ 
#define INTERNETSERVICE

using System;
using System.IO;
using System.Text;
using System.Web.Services.Description;
using System.Windows.Forms;
#if INTERNETSERVICE
using TestApplication.cz.jakubmaly.test;
#else 
using TestApplication.TravelAgentServiceSln;
#endif

namespace TestApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        readonly TravelAgentService TAService = new TravelAgentService(); 

        private void button1_Click(object sender, EventArgs e)
        {
            ConnectionRQType request = PrepareRequest();

            ConnectionRPType response = TAService.FindConnection(request);

            DisplayResponse(response);
        }

        private ConnectionRQType PrepareRequest()
        {
            ConnectionRQType request = new ConnectionRQType();

            request.from = new StationType { name = tbFrom.Text };
            request.to = new StationType { name = tbTo.Text };
            request.maxChanges = Convert.ToInt32(tbMaxChanges.Value);

            if (rbArrival.Checked)
            {
                request.ItemElementName = ItemChoiceType.arrivalTime;
            }
            else
            {
                request.ItemElementName = ItemChoiceType.departureTime;
            }
            
            request.Item = tbDateTime.Value;

            request.through = new StationType[tbThrough.Lines.Length];
            for (int index = 0; index < tbThrough.Lines.Length; index++)
            {
                string line = tbThrough.Lines[index];
                request.through[0] = new StationType { name = line };
            }

            return request; 
        }

        private void DisplayResponse(ConnectionRPType response)
        {
            string tempFileName = Path.GetTempFileName();
            tempFileName = Path.GetFileNameWithoutExtension(tempFileName) + ".xml";
            FileInfo f = new FileInfo(tempFileName);
            StreamWriter streamWriter = f.CreateText();
            streamWriter.Write(TAService.ResponseSoapContext.Envelope.OuterXml);
            streamWriter.Flush();

            webBrowser1.Navigate("file://" + f.FullName);
            
           // f.Delete();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
            tbDateTime.Value = DateTime.Now;
        }
    }
}
